package com.example.dzl.mybatisplustest.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.dzl.mybatisplustest.model.entity.User;
import com.example.dzl.mybatisplustest.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 光的代言人
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping("/page")
    public IPage<User> getUserPage(@RequestParam int pageNum, @RequestParam int pageSize){
        // pageNum:当前页 pageSize:每页显示的条数
        Page<User> page=new Page<>(pageNum,pageSize);
        return userService.page(page);
    }
    @GetMapping("{id}")
    public User getUserById(@PathVariable Integer id){
        return userService.getById(id);
    }
    @GetMapping("search")
    public List<User> getUserByName(@RequestParam String username){
        QueryWrapper<User> queryWrapper=new QueryWrapper<User>().select("username","sex","address").like("address","95").eq("sex","女");
        return userService.list(queryWrapper);
    }
    @PutMapping("update/{id}")
    public boolean updateUser(@PathVariable Integer id){
//        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
//        queryWrapper.eq("id",id);
//        User user=userService.getOne(queryWrapper);
//        user.setSex("男");
        LambdaQueryWrapper<User> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getId, id);
        User user=userService.getOne(queryWrapper);
        return userService.updateById(user);
    }

}
